﻿//using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils.Drawing;
using System;

namespace PTA.NET
{
    public partial class Main2 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        //private string SelectedImageKey;


        public Main2()
        {
            InitializeComponent();
        }

       

        FrmNhapAnswer frm;
        private void Main2_Load(object sender, EventArgs e)
        {
            frm = new FrmNhapAnswer();
            frm.MdiParent = this;
            frm.AnswerItemClicked += new FrmNhapAnswer.AnswerItemEventHandler(frm_AnswerItemClicked);
            frm.Show();

            // Load skin
            foreach (DevExpress.Skins.SkinContainer skin in DevExpress.Skins.SkinManager.Default.Skins)
            {
                BarCheckItem item = ribbonControl1.Items.CreateCheckItem(skin.SkinName, false);
                item.Tag = skin.SkinName;
                item.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(OnChangeSkinClick);
                btnChangeSkin.ItemLinks.Add(item);
            }

            //LoadTest();
        }

        void OnChangeSkinClick(object sender, ItemClickEventArgs e)
        {
            defaultLookAndFeel1.LookAndFeel.SetSkinStyle(e.Item.Tag.ToString());
        }

        void frm_AnswerItemClicked(int AnswerType, int Diem)
        {
            if (AnswerType == 0)
            {
                int Dung = int.Parse(lblTotalDung.Caption.Split(':')[1]) + Diem;
                lblTotalDung.Caption = "Đúng: " + Dung.ToString();
            }
            else if (AnswerType == 1)
            {
                int Sai = int.Parse(lblTotalSai.Caption.Split(':')[1]) + Diem;
                lblTotalSai.Caption = "Sai: " + Sai.ToString();
            }
            else if (AnswerType == 2)
            {
                int KhongBiet = int.Parse(lblTotalKhongBiet.Caption.Split(':')[1]) + Diem;
                lblTotalKhongBiet.Caption = "Không biết: " + KhongBiet.ToString();
            }
        }

        private void btnDung_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnSai.Down = false;
            btnKhongBiet.Down = false;
            frm.SelectedImageKey = Common.DUNG;
        }

        private void btnSai_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnDung.Down = false;
            btnKhongBiet.Down = false;
            frm.SelectedImageKey = Common.SAI;
        }

        private void btnKhongBiet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm.SelectedImageKey = Common.KHONG;
            btnSai.Down = false;
            btnDung.Down = false;
        }

        private void btnChangeSkin_Popup(object sender, EventArgs e)
        {
            foreach (BarItemLink link in btnChangeSkin.ItemLinks)
                ((BarCheckItem)link.Item).Checked = link.Item.Caption == defaultLookAndFeel1.LookAndFeel.ActiveSkinName;
        }

        

        private void btnListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.ChangeViewType(2);
        }

        private void btnLargeView_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.ChangeViewType(1);
        }

        private void btnSmallView_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.ChangeViewType(3);
        }

        private void btnTileView_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.ChangeViewType(4);
        }

        private void btnDetailView_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.ChangeViewType(0);
        }


        private  bool CreateNotExistFile(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                string parent = FilePath.Substring(0, FilePath.LastIndexOf("\\"));
                if (CheckFolder(parent))
                    File.WriteAllText(FilePath, @"<?xml version=""1.0"" encoding=""utf-8""?><configuration />");

                return false;
            }
            return true;
        }

        private bool CheckFolder(string Path)
        {
            if (!Directory.Exists(Path))
            {
                // Check parent folder
                string parent = Path.Substring(0, Path.LastIndexOf("\\"));
                if (CheckFolder(parent))
                {
                    Directory.CreateDirectory(Path);
                }
            }
            return true;
        }

        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            LuuKetQua(frm.CustomerItem);            
        }

        private void LuuKetQua(Customer CustomerItem)
        {
            SaveFileDialog saveFileDiaglog = new SaveFileDialog();
            saveFileDiaglog.Filter = "Data files (*.config)|*.config|All files (*.*)|*.*";
            saveFileDiaglog.FilterIndex = 0;
            saveFileDiaglog.RestoreDirectory = true;

            if (saveFileDiaglog.ShowDialog() == DialogResult.OK)
            {
                string temp = LuuTamLocal(CustomerItem, saveFileDiaglog.FileName.Substring(saveFileDiaglog.FileName.LastIndexOf("\\")));
                //File.Copy(temp, saveFileDiaglog.FileName.Substring(0, saveFileDiaglog.FileName.LastIndexOf("\\")));
                File.Move(temp, saveFileDiaglog.FileName);
            }
        }



        private string LuuTamLocal(Customer CustomerItem, string FileName)
        {
            string temp_folder = System.Configuration.ConfigurationSettings.AppSettings.Get("temp_folder");
            string file_name = temp_folder + FileName;

            CreateNotExistFile(file_name);

            AQFramework.Configurations config = new AQFramework.Configurations(file_name);
            CustomerItem.CustomerName = txtHoTen.EditValue.ToString();
            CustomerItem.GioiTinh = chkNam.Down ? true : false;
            CustomerItem.NgayLam = DateTime.Now;
            config.RemoveSectionsObject("Customer");
            config.WriteSectionsObject("Customer", CustomerItem);
            return file_name;
        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            CreateNewTest();
        }

        private void CreateNewTest()
        {
            txtHoTen.EditValue = "";
            lblTotalDung.Caption = "Đúng: 0";
            lblTotalSai.Caption = "Sai: 0";
            lblTotalKhongBiet.Caption = "Không biết: 0";
            frm.CustomerItem = new Customer();
        }

        private void LoadDataFile(string file_name)
        {
            AQFramework.Configurations config = new AQFramework.Configurations(file_name);
            Customer CustomerItem = new Customer();
            CustomerItem = (Customer)config.ReadSectionsObject("Customer");
            frm.CustomerItem = CustomerItem;
            txtHoTen.EditValue = CustomerItem.CustomerName;
            //frm.lsvAnswers.RefreshOverlays();
            //frm.lsvAnswers.RefreshHotItem();
            //frm.lsvAnswers.RefreshOverlays(); 
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            //AQFramework.Configurations config = new AQFramework.Configurations("test.config");
            //Customer CustomerItem = new Customer();
            //CustomerItem = (Customer)config.ReadSectionsObject("Customer");
            //frm.CustomerItem = CustomerItem;

            //foreach (Questions q in frm.CustomerItem.QuestionList)
            //{
            //    if (q.Answer == null) continue;
            //    if (q.Answer.Equals("SAI"))
            //        q.Answer = "K";
            //}

            //foreach (Questions q in frm.CustomerItem.QuestionList)
            //{
            //    if (q.Answer == null) continue;
            //    if (q.Answer.Equals("KHONG"))
            //        q.Answer = "SAI";
            //}

            //foreach (Questions q in frm.CustomerItem.QuestionList)
            //{
            //    if (q.Answer == null) continue;
            //    if (q.Answer.Equals("K"))
            //        q.Answer = "KHONG";
            //}

            ////AQFramework.Configurations config = new AQFramework.Configurations("test.config", true);
            //config.RemoveSectionsObject("Customer");
            //config.WriteSectionsObject("Customer", frm.CustomerItem);
        }



        private void ThongKe(Customer CustomerItem)
        {
            int Dung = 0, Sai = 0, Khong = 0;

            for (int i = 1; i <= 567; i++)
            {
                if (CustomerItem.QuestionList[i].Answer == null) continue;

                if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("dung"))
                    Dung++;
                else if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("sai"))
                    Sai++;
                else if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("khong"))
                    Khong++;
            }

            lblTotalDung.Caption = "Đúng: " + Dung.ToString();
            lblTotalKhongBiet.Caption = "Không Biết: " + Khong.ToString();
            lblTotalSai.Caption = "Sai: " + Sai.ToString();

        }

        private void btnXuLy_ItemClick(object sender, ItemClickEventArgs e)
        {
            FrmConfirm frmConfirm = new FrmConfirm();
            frm.CustomerItem.CustomerName = txtHoTen.EditValue.ToString();
            frm.CustomerItem.GioiTinh = chkNam.Down ? true : false;
            frmConfirm.CustomerItem = frm.CustomerItem;
            frmConfirm.CustomerItem.SinhNam = txtSinhNam.EditValue.ToString();
            frmConfirm.ShowDialog();
            //frm.lsvAnswers.Refresh();
            //frm.lsvAnswers.RefreshObjects(frm.CustomerItem.QuestionList);
        }

        private void chkNam_ItemClick(object sender, ItemClickEventArgs e)
        {
            chkNu.Down = false;
        }

        private void chkNu_ItemClick(object sender, ItemClickEventArgs e)
        {
            chkNam.Down = false;
        }

        private void btnNewBN_ItemClick(object sender, ItemClickEventArgs e)
        {
            Common.CustomerItem = new Customer();
            Common.CustomerItem.CustomerName = txtHoTen.EditValue.ToString();
            Common.CustomerItem.GioiTinh = chkNam.Down ? true : false;
            Common.CustomerItem.SinhNam = txtSinhNam.EditValue.ToString();

            TracNghiem frmTracNghiem = new TracNghiem();
            frmTracNghiem.ShowDialog();

            //if (frmTracNghiem.STT >= 567)
            {
                FrmConfirm frmConfirm = new FrmConfirm();
                frmConfirm.CustomerItem = Common.CustomerItem;
                frm.CustomerItem = Common.CustomerItem;
                frmConfirm.ShowDialog();
            }
        }

        private void txtHoTen_EditValueChanged(object sender, EventArgs e)
        {
            frm.Text = txtHoTen.EditValue.ToString();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            //.string/LoadTe//st();
        }

        private void btnLoad_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = @"\\BenhNhan";
            openFileDialog1.Filter = "Data files (*.config)|*.config|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string file_name = openFileDialog1.FileName;
                    LoadDataFile(file_name);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
                ThongKe(frm.CustomerItem);
            }

        }

        private void btnSaveBN_ItemClick(object sender, ItemClickEventArgs e)
        {
            LuuKetQua(Common.CustomerItem);
        }
    }

    

}
